package admin_auth

import (
	"encoding/json"
	"insight/internal/model"
	"insight/internal/pkg/errors"
	"insight/internal/resources"
	"insight/internal/service"
)

// AdminUserService 授权服务
type AdminUserService struct {
	service.Base
}

func NewAdminUserService() *AdminUserService {
	return &AdminUserService{}
}

// GetUserInfo 获取用户信息
func (s *AdminUserService) GetUserInfo(id uint) (*resources.AdminUserResources, error) {
	// 查询用户是否存在
	adminUsersModel := model.NewAdminUsers()
	user := adminUsersModel.GetUserById(id)
	if user != nil {
		result := resources.NewAdminUserResources(*user)
		var roles []string
		if err := json.Unmarshal(user.Roles, &roles); err != nil {
			return nil, errors.NewBusinessError(errors.FAILURE, "获取用户信息失败")
		}
		result.SetRoles(roles)
		return result, nil
	}
	return nil, errors.NewBusinessError(errors.FAILURE, "获取用户信息失败")
}
